package com.vne.skyemart.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.vne.skyemart.model.Cart;
import com.vne.skyemart.service.CartService;

@Controller
@SessionAttributes
@RequestMapping("/cart")
public class CartController {

	@Autowired
	private CartService cartService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView saveCart(@ModelAttribute(" cart") Cart cart,
			BindingResult result) {
		 cartService.addCart(cart);
		return new ModelAndView("redirect:/cart.html");
	}

	@RequestMapping(method = RequestMethod.GET)
	public String listCart(Model model) {
		model.addAttribute("cart",  cartService.listCart());

		return "CartList";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addCart(@ModelAttribute("cart") Cart cart,
			BindingResult result) {
		return new ModelAndView("addCart");
	}
}